package com.cy.demo1.algorithm.miamiayyds.Class;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Objects;

//public class Judge extends JPanel
public class Judge
 {
     public float acc;
     public float pre;
     public float rec;
     public float f1;
     public float auc;
    public static double f[][] = new double[700][2];
    public void score(ArrayList<String> type_test,ArrayList<String> type_result)
    {
        double a1 = 0;

        int N = 0;
        int P = 0;
        int TN = 0;
        int TP = 0;

        int TP1 =0;
        int P1 = 0;

            for (int b1 = 0;b1<10;b1++)
            {
                for (int b = 0;b<type_test.size();b++)
                {
                    if (Objects.equals(type_test.get(b), "buggy"))
                    {
                        N++;
                        if (Objects.equals(type_test.get(b), type_result.get(b)))
                        {
                            //正确诊断出bug
                            TN++;
                        }
                    }
                    else if (Objects.equals(type_test.get(b), "clean"))
                    {
                        P++;
                        P1++;
                        if (Objects.equals(type_test.get(b), type_result.get(b)))
                        {
                            //正确诊断出clean
                            TP++;
                            TP1++;
                        }

                    }
                }

                f[b1][0] = (float)TP1/(N-TN+TP1);
                f[b1][1] = (float)(P1-TP1)/(P1-TP1+TN);
                TP1 = 0;
                P1 = 0;
            }
            for (int b2 = 0;b2<10;b2++)
            {
                a1 = a1 + (f[b2][1]+f[b2+1][1])*(f[b2][0]-f[b2+1][0])/2;
                System.out.println(f[b2][1]);
                System.out.println(f[b2+1][1]);
                System.out.println(f[b2][0]);
                System.out.println(f[b2+1][0]);
            }

            //System.out.println("AUC面积:"+(1-a1));



        DecimalFormat dF = new DecimalFormat("0.00000");  //精确度
        // System.out.println("TP为 :"+dF.format((float)TP));
        // System.out.println("FN为 :"+dF.format((float)(N-TN)));
        // System.out.println("FP为 :"+dF.format((float)(P-TP)));
        // System.out.println("TN为 :"+dF.format((float)TN));
        // System.out.println("N为 :"+dF.format((float)N));
        // System.out.println("P为 :"+dF.format((float)(P)));

        float precision;//精确率
        float recall;//召回率
        float accuracy;//准确率
        float TPR;//TPR = TP / (TP + FN)
        float FPR;//FPR = FP / (TN + FP)
        float F1; //2*precision*recall/(precision+recall)
        float area;

        accuracy = (float)(TP+TN)/(N+P);
        precision = (float)TP/P;
        recall = (float)TP/(N-TN+TP);
        TPR = (float)TP/(N-TN+TP);
        FPR = (float)(P-TP)/(TN+P-TP);
        F1 = (float)2*precision*recall/(precision+recall);
        area = (float) (1-a1);


        System.out.println("准确率为 :"+accuracy);
        System.out.println("精确率为 :"+precision);
        System.out.println("召回率为 :"+recall);
        System.out.println("真正率为 :"+TPR);
        System.out.println("假正率为 :"+FPR);
        System.out.println("F1为 :"+F1);
        System.out.println("AUC面积为 :"+area);

        acc = accuracy;
        pre = precision;
        rec = recall;
        f1 = F1;
        auc = area;
    }
    
    
    /*public void roc()
    {
        //Graphics g  = getGraphics();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // frame.setSize(50000000, 50000000);
        frame.setSize(500, 500);
        Judge a1 = new Judge();
        //frame.add(a1);
        frame.setTitle("ROC曲线");
        frame.setVisible(true);
        savePic(frame);
    }*/
    

    public void paintComponent(Graphics g)
    {
       g.setColor(Color.black);
       g.drawLine(10, 10, 10, 450);
       g.drawLine(10, 450, 450, 450);
       for(int i = 0;i<10;i++)
       {
           g.drawLine(10+(int)(f[i][1]*440), 10+(int)((1-f[i][0])*440), 10+(int)(f[i+1][1]*440), 10+(int)((1-f[i+1][0])*440));
       }	
      
    }
    public void snapshot()
    {
        try 
        {
            Robot robot=new Robot();
            //根据指定的区域抓取屏幕的指定区域
            BufferedImage bi=robot.createScreenCapture(new Rectangle(500,500));
            //把抓取到的内容写到一个jpg文件中
            ImageIO.write(bi, "png", new File("2.png"));
        } 
        catch (AWTException e)
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }     
    }
    //没用，假的，但提供缓冲时间
    public void savePic(JFrame jf)
    {
		//得到窗口内容面板
		Container content=jf.getContentPane();
		//创建缓冲图片对象
		BufferedImage img=new BufferedImage(
				jf.getWidth(),jf.getHeight(),BufferedImage.TYPE_INT_RGB);
		//得到图形对象
		Graphics2D g2d = img.createGraphics();
		//将窗口内容面板输出到图形对象中
		content.printAll(g2d);
		//保存为图片
		File f=new File("saveScreen.jpg");
		try 
        {
			ImageIO.write(img, "jpg", f);
		} 
        catch (IOException e) 
        {
			e.printStackTrace();
		}
		//释放图形对象
		g2d.dispose();
	}

      
    }
   
     
       
    
                      
    

   
    

